﻿using HZU.ZP.Data.Interfaces;
using HZU.ZP.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HZU.ZP.Data.Impl
{
    public class ZanDao:IZanDao
    {
        private EFCoreDbContext _context;
        public ZanDao(EFCoreDbContext context)
        {
            _context = context;
        }

        public void Add(Zan zan)
        {
            _context.Zans.Add(zan);
            _context.SaveChanges();
        }

        public void Delete(int id)
        {
            var zan = _context.Zans.FirstOrDefault(e => e.Id == id);
            _context.Zans.Remove(zan);
            _context.SaveChanges();
        }

        public Zan GetZan(int noticeId, int userId, string roleName)
        {
            return _context.Zans.FirstOrDefault(e => e.NoticeId == noticeId && e.UserId == userId && e.RoleName == roleName);
        }

        public int GetZanCount(int noticeId)
        {
            return _context.Zans.Where(e => e.NoticeId == noticeId).Count();
        }
    }
}
